package org.csu.webjpetstoreback.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.model.Account;
import org.csu.webjpetstoreback.model.Profile;
import org.csu.webjpetstoreback.model.Signon;
import org.csu.webjpetstoreback.model.Token;
import org.csu.webjpetstoreback.service.AccountService;
import org.csu.webjpetstoreback.service.ProfileService;
import org.csu.webjpetstoreback.service.SignonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController

@Validated
public class SignonController {

    @Autowired
    private SignonService signonService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private ProfileService profileService;

    @Autowired
    private  Account account;

    @Autowired
    private Signon signon;

    @Autowired
    private Profile profile;


    @GetMapping("/login")
    public CommonResponse<List<Account>> login(@RequestParam("username") String username,@RequestParam("password") String password, HttpSession httpSession){
        CommonResponse<Signon> commonResponse= signonService.login(username, password);
        if (commonResponse.getStatus() == 200){
            //开始创建一个tokens
            List<Account> accounts = new ArrayList<>();
            Account account = accountService.GetAccountByID(username).getData();
            accounts.add(account);
            Profile profile = profileService.GetAccountByID(username).getData();
            Token token = new Token(account,commonResponse.getData(),profile);
            httpSession.setAttribute("token",token);
            System.out.println("hello login");
            System.out.println("account is:"+account);
            System.out.println("token is:  "+token);
            httpSession.setAttribute("account",account);
            httpSession.setAttribute("profile",profile);
            httpSession.setAttribute("token",token);
            return CommonResponse.BackForSuccess(accounts);
        }else{
            return CommonResponse.BackForError(commonResponse.getMessage());
        }
    }

    @DeleteMapping("/tokens")
    public  CommonResponse<Token> logoff(HttpSession httpSession){
        httpSession.removeAttribute("token");
        return CommonResponse.BackForSuccess("退出登录成功！！");
    }

    @PostMapping("/Account")
    public CommonResponse<Signon> register (@RequestBody Token token){
        account.setAccount(token);
        CommonResponse<Account> commonResponse = accountService.PostAccount(account);
        //
        System.out.println(account);
        System.out.println(commonResponse);
        //
        profile.setProfile(token);
        CommonResponse<Profile> commonResponse1 = profileService.PostAccount(profile);
        //
        System.out.println(profile);
        System.out.println(commonResponse1);
        //
        signon.setSignon(token);
        CommonResponse<Signon> commonResponse2 = signonService.PostAccount(signon);
        System.out.println(commonResponse2);
        if (commonResponse.getStatus()==0 && commonResponse1.getStatus()==0 && commonResponse2.getStatus() == 0){
            return CommonResponse.BackForSuccess("注册成功");
        }else{
            return CommonResponse.BackForError("注册失败!!");
        }
    }

    @GetMapping("Account/{id}")
    public CommonResponse<Account> getAccountName(@PathVariable("id") String userid){
        return accountService.GetAccountByID(userid);
    }

}
